const app = getApp();
const common = require('../../../utils/common.js');
const baseUrl = app.globalData.baseUrl;
Page({
	data: {
		res: []
	},
	onLoad: function () {
		var Uid = app.globalData.loginInfo.Uid;
		var that = this;
		let baseUrl = app.globalData.baseUrl;
		wx.request({
			url: baseUrl + '/api/dynamic/getCommentByUid',
			data: {
				Uid: Uid
			},
			method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
			// header: {}, // 设置请求的 header
			success: function (res) {
				that.setData({
					res: res.data.data
				});
				// success
			},
			fail: function () {
				// fail
			},
			complete: function () {
				// complete
			}
		});
	},
	likeAction: function (e) {
		let Did = e.target.dataset.id;
		let Uid = app.globalData.loginInfo.Uid;
		let needindex = common.searchMember(this.data.res, 'message', 'id', e.target.dataset.id);
		let changeLike = "res[" + needindex + "].message.hasLike",
			changeNum = "res[" + needindex + "].message.likeNum";
		let state = !this.data.res[needindex].message.hasLike,
			num = this.data.res[needindex].message.likeNum;
		if (state) {
			//cancel
			num ++;
			wx.request({
				url: baseUrl + '/api/dynamic/setlike',
				data: {
					Did : Did,
					Uid : Uid
				},
				method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
				// header: {}, // 设置请求的 header
				success: function(res){
					console.log(res);
					// success
				},
				fail: function() {
					// fail
				},
				complete: function() {
					// complete
				}
			});
		} else {
			//add
			num --;
			wx.request({
				url: baseUrl + '/api/dynamic/cancellike',
				data: {
					Did : Did,
					Uid : Uid
				},
				method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
				// header: {}, // 设置请求的 header
				success: function(res){
					console.log(res);
					// success
				},
				fail: function() {
					// fail
				},
				complete: function() {
					// complete
				}
			});
		}
		this.setData({
			[changeLike]: state,
			[changeNum] : num
		});
	},
	commentAction: function (e) {
		console.log(e.target.dataset.id);
	}

});